package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         199,
		Discription: "二叉树的右视图",
		Level:       2,
		Labels: map[string]int{
			"二叉树":  1,
			"层序遍历": 1,
		},
	})
}

func RightSideView(root *ds.TreeNode) []int {
	res := make([]int, 0)
	if root == nil {
		return res
	}

	q := []*ds.TreeNode{root}
	for len(q) != 0 {
		length := len(q)
		res = append(res, q[length-1].Val)

		for i := 0; i < length; i++ {
			node := q[i]
			if node.Left != nil {
				q = append(q, node.Left)
			}
			if node.Right != nil {
				q = append(q, node.Right)
			}
		}

		q = q[length:]
	}

	return res
}
